//
//  VCOfMine.m
//  XYFMApp
//
//  Created by 岁变 on 16/8/27.
//  Copyright © 2016年 岁变. All rights reserved.
//

//#import "VCOfMine.h"
//#import "UIView+Frame.h"
#import "XYMacro.h"

@interface VCOfMine ()
@property (nonatomic, strong) UIButton *loginButton;
@property (nonatomic, strong) UIImageView *headerImageView;
@property (nonatomic, strong) UIButton *registerButton;
@property (nonatomic, strong) UIButton *forgetButton;
@property (nonatomic, strong) UITextField *phoneNumberField;
@property (nonatomic, strong) UITextField *passwordField;

@property (nonatomic, strong) NSString *promptString;
@property (nonatomic, strong) ModelOfMineTwo *model;

@end

@implementation VCOfMine

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configNavigationController];
    [self configViewController];
    
}

- (void)sendLoadinMessage {
    
    NSString *str1 = @"json=%7B%22password%22%3A%22";
    NSString *str2 = @"%22%2C%22username%22%3A%22";
    NSString *str3 = @"%22%7D";
    NSString *body = [[[[str1 stringByAppendingString:_passwordField.text] stringByAppendingString:str2] stringByAppendingString:_phoneNumberField.text] stringByAppendingString:str3];
    
    [XYNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/user/signin.jhtml" andBody:body andBodyStyle:XYBodyString andHeader:URLHEADER andResponse:XYJSON andSuccessBlock:^(id result) {
            _model = [ModelOfMineTwo modelWithDictionary:result];
        if (_model.status.succeed == 0) {
            _promptString = _model.status.error_desc;
            [self createPromptBox];
        } else {
            //NSUserDefaults 记录有用值
            NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
            [user setObject:_passwordField.text forKey:@"password"];
            [user setObject:_phoneNumberField.text forKey:@"phoneNumber"];
            NSString *target_id = [NSString stringWithFormat:@"%ld", _model.user.id];
            [user setObject:target_id forKey:@"target_id"];
            
            VCOfRegisterThird *nextVC = [[VCOfRegisterThird alloc] init];
            nextVC.name = _model.user.nickname;
            nextVC.age = [NSString stringWithFormat:@"%ld", _model.user.age];
            nextVC.brithday = _model.user.birthday;
            nextVC.constellation = _model.user.constellation;
            if ([_model.user.gender isEqualToString:@"1"]) {
                nextVC.gender = @"男";
            } else {
                nextVC.gender= @"女";
            }
            [self.navigationController pushViewController:nextVC animated:YES];
        }
        
    } andFailureBlock:^(NSError *error) {}];

    
}


- (void)configNavigationController {
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.00 green:0.00 blue:0.06 alpha:1.00];
    _loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
    [_loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _loginButton.backgroundColor = [UIColor colorWithRed:0.94 green:0.00 blue:0.00 alpha:1.00];
    _loginButton.frame = CGRectMake(0, 0, 200, 30);
    _loginButton.titleLabel.font = [UIFont systemFontOfSize:14];
    _loginButton.layer.cornerRadius = 10;
    _loginButton.layer.borderWidth = 1;
    _loginButton.layer.borderColor = [UIColor redColor].CGColor;
    [_loginButton addTarget:self action:@selector(clickRegister:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.titleView = _loginButton;
    
}

- (void)configViewController {
    self.view.backgroundColor = [UIColor colorWithRed:0.09 green:0.09 blue:0.12 alpha:1.00];
    
    
    _headerImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"default_user"]];
    [self.view addSubview:_headerImageView];
    [_headerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).with.offset(SCREEN_HEIGHT / 20);
        make.height.mas_offset(60);
        make.width.mas_offset(60);
        make.centerX.mas_equalTo(self.view.mas_centerX);
    }];
    _headerImageView.clipsToBounds = YES;
    _headerImageView.layer.cornerRadius = 30;
    _headerImageView.layer.borderWidth = 2;
    _headerImageView.layer.borderColor = [UIColor redColor].CGColor;
    
    UIImageView *messageImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"login_input_bg"]];
    messageImageView.userInteractionEnabled = YES;
    [self.view addSubview:messageImageView];
    [messageImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).with.offset(SCREEN_HEIGHT / 5);
        make.height.mas_equalTo(100);
        make.left.equalTo(self.view.mas_left).with.offset(0);
        make.right.equalTo(self.view.mas_right).with.offset(0);
    }];
    
    _registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_registerButton addTarget:self action:@selector(pushNextPage:) forControlEvents:UIControlEventTouchUpInside];
    [messageImageView addSubview:_registerButton];
    [_registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(messageImageView.mas_left).with.offset(0);
        make.width.mas_equalTo(messageImageView.frame.size.width / 9);
        make.height.mas_equalTo(40);
        make.centerY.mas_equalTo(messageImageView.mas_centerY);
    }];
    [_registerButton setTitle:@"注册" forState:UIControlStateNormal];
    [_registerButton setTitleColor:[UIColor colorWithRed:0.91 green:0.00 blue:0.00 alpha:1.00] forState:UIControlStateNormal];
    _registerButton.titleLabel.font = [UIFont systemFontOfSize:14];
    
    _forgetButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [messageImageView addSubview:_forgetButton];
    [_forgetButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(messageImageView.mas_right).with.offset(0);
        make.width.mas_equalTo(messageImageView.frame.size.width / 9);
        make.height.mas_equalTo(40);
        make.centerY.mas_equalTo(messageImageView.mas_centerY);
    }];
    [_forgetButton setTitle:@"忘记密码" forState:UIControlStateNormal];
    [_forgetButton setTitleColor:[UIColor colorWithRed:0.91 green:0.00 blue:0.00 alpha:1.00] forState:UIControlStateNormal];
    _forgetButton.titleLabel.font = [UIFont systemFontOfSize:14];
    _forgetButton.titleLabel.numberOfLines = 2;
    
    _phoneNumberField = [[UITextField alloc] init];
    _phoneNumberField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"手机号码" attributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:0.25 green:0.30 blue:0.36 alpha:1.00]}];
    _phoneNumberField.textColor = [UIColor whiteColor];
    [messageImageView addSubview:_phoneNumberField];
    [_phoneNumberField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(messageImageView.mas_top).with.offset(0);
        make.left.equalTo(messageImageView.mas_left).with.offset(messageImageView.frame.size.width / 5);
        make.right.equalTo(messageImageView.mas_right).with.offset(-(messageImageView.frame.size.width / 5));
        make.height.mas_equalTo(50);
    }];
    
    _passwordField = [[UITextField alloc] init];
    _passwordField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"密码" attributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:0.25 green:0.30 blue:0.36 alpha:1.00]}];;
    _passwordField.textColor = [UIColor whiteColor];
    [messageImageView addSubview:_passwordField];
    [_passwordField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(messageImageView.mas_top).with.offset(50);
        make.left.equalTo(messageImageView.mas_left).with.offset(messageImageView.frame.size.width / 5);
        make.right.equalTo(messageImageView.mas_right).with.offset(-(messageImageView.frame.size.width / 5));
        make.height.mas_equalTo(50);
    }];
    
    
    
    
    UIImageView *otherLoginImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"login_other_line"]];
    [self.view addSubview:otherLoginImageView];
    [otherLoginImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).with.offset(SCREEN_HEIGHT / 5 * 2.7);
        make.width.mas_equalTo(240);
        make.height.mas_equalTo(80);
        make.centerX.mas_equalTo(self.view.mas_centerX);

    }];
    
    
}


- (void)pushNextPage:(UIButton *)button {
    VCOfRegister *vcOfRegister = [[VCOfRegister alloc] init];
    [self.navigationController pushViewController:vcOfRegister animated:YES];
    
}

- (void)clickRegister:(UIButton *)button {
    if (_phoneNumberField.text.length != 11) {
        _promptString = @"请输入11位手机号";
        [self createPromptBox];
        return;
    }
    if (_phoneNumberField.text.length == 11 && _passwordField.text.length == 0) {
        _promptString = @"密码不可以为空";
        [self createPromptBox];
        return;
    }
    [self sendLoadinMessage];
    
}


#pragma mark -----------创建自定义提示栏-----------------
- (void)createPromptBox {
    UIWindow *windows = [UIApplication sharedApplication].keyWindow;
    UIView *customPromptBox = [[UIView alloc]init];
    customPromptBox.backgroundColor = [UIColor colorWithRed:0.18 green:0.18 blue:0.18 alpha:1.00];
    [windows addSubview:customPromptBox];
    [customPromptBox mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.centerY.mas_equalTo(self.view.mas_centerY);
        make.width.mas_offset(260);
        make.height.mas_offset(100);
    }];
    customPromptBox.layer.masksToBounds = YES;
    customPromptBox.layer.cornerRadius = 8.0f;
    
    UIImageView *logoImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon"]];
    [customPromptBox addSubview:logoImageView];
    [logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(customPromptBox.mas_top).with.offset(10);
        make.centerX.equalTo(customPromptBox.mas_centerX);
        make.height.mas_offset(40);
        make.width.mas_offset(40);
    }];
    
    UILabel *textLabel = [[UILabel alloc] init];
    textLabel.text = _promptString;
    textLabel.font = [UIFont systemFontOfSize:14];
    textLabel.textColor = [UIColor whiteColor];
    textLabel.textAlignment = NSTextAlignmentCenter;
    [customPromptBox addSubview:textLabel];
    [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(logoImageView.mas_bottom).with.offset(5);
        make.bottom.equalTo(customPromptBox.mas_bottom).with.offset(-5);
        make.left.equalTo(customPromptBox.mas_left).with.offset(0);
        make.right.equalTo(customPromptBox.mas_right).with.offset(0);
    }];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)),dispatch_get_main_queue(), ^{
        //2秒以后移除view
        [customPromptBox removeFromSuperview];
    });
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
